import 'dart:io';

import 'package:dio/dio.dart';
import 'package:macnov/utils/result_data.dart';
import 'http_manager.dart';
import 'address.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Api {
  static Future<String> getPrefix() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var et = prefs.getString("et");
    if (et == 'h0*6wr') return 'owner';
    if (et == r'W16j$P') return 'employee';
    return '';
  }

  static Future<bool> isOwner() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var et = prefs.getString("et");
    return et == 'h0*6wr';
  }

  // user
  static Future<bool> doLogin(Map<String, String> params) async {
    var response = await HttpManager.getInstance().login(Address.LOGIN, params);
    var headers = response.headers;
    if (headers != null) {
      var authorization = headers['authorization'][0];
      var et = headers['et'][0];
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString("token", authorization);
      prefs.setString("et", et);
      return Api.isOwner();
    }
    return null;
  }

  static Future<bool> signOut() async {
    ResultData userResponse =
        await HttpManager.getInstance().post(Address.signOut, {});
    return userResponse.code == 200;
  }

  static Future<dynamic> loadUserSetting() async {
    var preFix = await getPrefix();
    ResultData userResponse = await HttpManager.getInstance()
        .post(preFix + Address.OWNER_USER_SETTING, {});
    return userResponse.data;
  }

// card
  static Future<dynamic> loadAllCards() async {
    var preFix = await getPrefix();

    ResultData cardResponse = await HttpManager.getInstance()
        .post(preFix + Address.OWNER_LOAD_ALL_CARDS, {});
    return cardResponse.data;
  }

  static Future<dynamic> loadVCards() async {
    var preFix = await getPrefix();

    ResultData cardResponse = await HttpManager.getInstance()
        .post(preFix + Address.OWNER_LOAD_VCARDS, {});
    return cardResponse.data;
  }

  static Future<dynamic> loadPhysicalCard() async {
    var preFix = await getPrefix();
    ResultData cardResponse = await HttpManager.getInstance()
        .post(preFix + Address.OWNER_PHYSICAL_CARD, {});
    var card = cardResponse.data;
    var cardInfo = await loadCardInfo({"id": "${card['cardId']}"});
    return cardInfo;
  }

  static Future<dynamic> loadCardInfo(params) async {
    var preFix = await getPrefix();
    ResultData cardInfoResponse = await HttpManager.getInstance()
        .get(preFix + Address.OWNER_GET_CARD_INFO, params);
    return cardInfoResponse.data;
  }

// transaction
  static Future<dynamic> loadTxn() async {
    var preFix = await getPrefix();

    ResultData txnResponse = await HttpManager.getInstance()
        .post(preFix + Address.OWNER_PENGING_CLEAR_TXN, {});
    return txnResponse.data;
  }

// budget
  static Future<dynamic> getCompanyActiveBudgets() async {
    var preFix = await getPrefix();
    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    try {
      ResultData budgetResponse = await HttpManager.getInstance()
          .get(preFix + Address.OWNER_ACTIVE_BUDGETS, queryParameters);
      return budgetResponse.data;
    } catch (e) {}
  }

  //
  static Future<dynamic> getMyActiveMemberBudgets() async {
    var preFix = await getPrefix();

    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    ResultData budgetResponse = await HttpManager.getInstance()
        .get(preFix + Address.getMyActiveMemberBudgets, queryParameters);
    return budgetResponse.data;
  }

  //
  static Future<dynamic> getAvatar() async {
    var preFix = await getPrefix();
    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    ResultData response = await HttpManager.getInstance()
        .get(preFix + Address.getAvatar, queryParameters);
    return response.data;
  }

  static Future<dynamic> getCompanySetting() async {
    var preFix = await getPrefix();
    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.getCompanySetting, queryParameters);
    return response.data;
  }

  static Future<dynamic> loadOwnerCompanyAccountInfo() async {
    var preFix = 'owner';
    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    ResultData resultData = await HttpManager.getInstance()
        .post(preFix + Address.loadOwnerCompanyAccountInfo, queryParameters);
    return resultData.data;
  }

  static Future<dynamic> getCreditInfo() async {
    var preFix = 'owner';
    Map<String, dynamic> queryParameters = new Map<String, dynamic>();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.getCreditInfo, queryParameters);
    return response.data;
  }

  static Future<dynamic> getMonthlyPayment(month) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance().post(
        preFix + Address.getMonthlyPayment + '?month=' + month, {"month": "1"});
    return response.data;
  }

  static Future<dynamic> getMonthlyPaymentCategory(month) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance().post(
        preFix + Address.getMonthlyPaymentCategory + '?month=' + month, {});
    return response.data;
  }

  static Future<dynamic> getReceipt(fileName) async {
    var preFix = await getPrefix();
    Response<dynamic> response = await HttpManager.getInstance()
        .getReceipt(preFix + Address.getReceipt, {"fileName": fileName});
    return response.data;
  }

  static Future<dynamic> uploadTxnReceipt(File file, String txnId) async {
    var preFix = await getPrefix();
    FormData formData = FormData.fromMap({
      "file": MultipartFile.fromFileSync(file.path, filename: 'txn.jpg'),
      "txn": txnId
    });
    Response<dynamic> response = await HttpManager.getInstance()
        .upload(preFix + Address.uploadTxnReceipt, formData);
    return response.data;
  }

  static Future<dynamic> uploadAvatar(File file) async {
    var preFix = await getPrefix();
    FormData formData = FormData.fromMap({
      "file": MultipartFile.fromFileSync(file.path, filename: 'avatar.jpg'),
    });
    Response<dynamic> response = await HttpManager.getInstance()
        .upload(preFix + Address.uploadAvatar, formData);
    return response.data;
  }

  static Future<dynamic> showVcardAuth(String cardId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.showVcardAuth, {"id": cardId});
    return response.data;
  }

  static Future<dynamic> getCardInfo(String cardId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .get(preFix + Address.getCardInfo, {"id": cardId});
    if (response.isSuccess) return response.data;
    return null;
  }

  static Future<dynamic> getBudgetPeopleCards(String budgetId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .get(preFix + Address.getBudgetPeopleCards, {"budgetId": budgetId});
    if (response.isSuccess) return response.data;
    return null;
  }

  static Future<dynamic> addVirtualCard(String nickName) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance().post(
        preFix + Address.addVirtualCard,
        {"nickName": nickName, "limit": '0.01'});
    if (response.isSuccess) return response.data;
    return null;
  }

  static Future<dynamic> deleteCard(String cardId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .get(preFix + Address.deleteCard, {"id": cardId});
    if (response.isSuccess) return response.data;
    return null;
  }

  static Future<dynamic> getCompanyUsers() async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.getCompanyUsers, {});
    return response.data;
  }

  static Future<dynamic> createBudget(formData) async {
    ResultData response =
        await HttpManager.getInstance().post(Address.createBudget, formData);
    return response.data;
  }

  static Future<dynamic> deleteBudget(String budgetId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .get(preFix + Address.deleteBudget, {"id": budgetId});
    if (response.isSuccess) return response.data;
    return null;
  }

  static Future<dynamic> editBudget(formData) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.editBudget, formData);
    return response.data;
  }

  static Future<dynamic> expireCard(String cardId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.expireCard, {"cardId": cardId});
    return response.data;
  }

  static Future<dynamic> activeCard(String cardId) async {
    var preFix = await getPrefix();
    ResultData response = await HttpManager.getInstance()
        .post(preFix + Address.activeCard, {"cardId": cardId});
    return response.data;
  }
}
